#pragma once

#include <windows.h>
#include <windowsx.h>
#include <stdio.h>

// Media Foundation 
#include <mfapi.h>
#include <mfidl.h>
#include <mfreadwrite.h>
#include <mferror.h>
// Direct Sound
#include <dsound.h>
// Utils
#include <propvarutil.h>

#include "mfcommon.h"
#include "thread.hpp"

#define MAX_AUDIO_BUF 4

class CAudioPlayer {

public:

	enum STATUS {
		Unknow,
		Stopped,
		Paused,
		Playing
	};

	CAudioPlayer();
	virtual ~CAudioPlayer();

	HRESULT Prepare(const WCHAR* wszFileName, HWND hwnd);
	HRESULT Play();
	HRESULT Pause();
	HRESULT Stop();
	HRESULT Seek(LONGLONG position);
	HRESULT SetVolume(long vol);
	HRESULT SetLoop(bool loop);

	STATUS  GetStatus() { return m_Status; }
	void Release();


private:
	HRESULT SeekTo();
	bool                m_bCanseek;
	bool				m_bLoop;
	LONGLONG			m_nSeekTo = -1;
	IMFSourceReader		*m_pReader = NULL;
	WAVEFORMATEX		*m_pWav = NULL;

	IDirectSound8		*m_pDS = NULL;
	IDirectSoundBuffer8 *m_pDSBuffer8 = NULL; //used to manage sound buffers.  
	IDirectSoundBuffer	*m_pDSBuffer = NULL;
	IDirectSoundNotify8 *m_pDSNotify = NULL;

	DSBPOSITIONNOTIFY	m_pDSPosNotify[MAX_AUDIO_BUF];
	HANDLE				m_event[MAX_AUDIO_BUF];

	STATUS              m_Status;

	QKThread			*m_thread = NULL;
	QKSignal			m_evtStatus;
	static unsigned int _stdcall ThreadStub(void* pData);

	unsigned int run();
}; 
